﻿using API;
using API.Store;
using Common;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class WarehouseBLL
    {
        public XPCollection<B_Warehouse> GetWarehouses(string storeID) 
        {
            XPCollection<B_Warehouse> Warehouses = null;
            if (MemberInfo.StoreCode == "LF01" || MemberInfo.StoreCode == "LF02") 
            {
                 Warehouses = new XPCollection<B_Warehouse>(DatabaseSchema.Session);
            }
            else
            {
                Warehouses = new XPCollection<B_Warehouse>(DatabaseSchema.Session, CriteriaOperator.Parse("StoreID=?", storeID));
            }
            return Warehouses;
        }
        public void BatchInsert()
        {
            try
            {
                ApiStore dataProxy = ServiceProxy.ApiStoreProxy();
                MB_Warehouse[] warehouses = dataProxy.GetWarehouses(null);
                if (warehouses == null || warehouses.Length <= 0) return;
                using (UnitOfWork uow = DatabaseSchema.UnitOfWork)
                {
                    foreach (MB_Warehouse w in warehouses)
                    {
                        B_Warehouse warehouse = new B_Warehouse(uow);
                        warehouse.WarehouseID = w.WarehouseID;
                        warehouse.WarehouseName = w.WarehouseName;
                        warehouse.StoreID = uow.GetObjectByKey<B_Store>(w.StoreID);
                        warehouse.StatusID = w.StatusID;
                        warehouse.Sort = w.Sort;
                        warehouse.Creator = w.Creator;
                        warehouse.CreateDT = w.CreateDT;
                        warehouse.Operator = w.Operator;
                        warehouse.UpdateDT = w.UpdateDT.Value;
                        warehouse.Remark = w.Remark;
                        warehouse.Save();
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("WarehouseBLL.BatchInsert", ex);
            }
        }
    }
}
